package com.tour.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Commodity;
import com.tour.entity.Shop;
import com.tour.mapper.CommodityMapper;
import com.tour.mapper.SCommentMapper;
import com.tour.mapper.ShopMapper;
import com.tour.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tour.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mysql.jdbc.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-30
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Autowired
    ShopMapper shopMapper;
    @Autowired
    SCommentMapper sCommentMapper;
    @Autowired
    CommodityMapper commodityMapper;

    public ResponseWrapper addShop(Shop shop){
        if (shop == null){
            return ResponseWrapper.markError();
        }else {
            int result = shopMapper.insert(shop);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else{
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper updateShop(Shop shop){
        if (shop == null){
            return ResponseWrapper.markError();
        }else {
            int result = shopMapper.updateById(shop);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else{
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper findShop(String name,Integer pageSize,Integer pageNum){
        QueryWrapper<Shop> shopQueryWrapper = new QueryWrapper<>();
        shopQueryWrapper.like(!StringUtils.isNullOrEmpty(name),"s_name",name).orderByDesc("s_score");
        List<Shop> list = shopMapper.selectList(shopQueryWrapper);
        ArrayList<Object> dateList = new ArrayList<>();
        for (Shop shops : list){
            HashMap<String, Object> shopmap = new HashMap<>();
          List<Commodity>  commodity = new ArrayList<>();
          QueryWrapper<Commodity> commodityQueryWrapper = new QueryWrapper<>();
          commodityQueryWrapper.eq("s_id",shops.getSId()).last("limit 0 , 3");
          List<Commodity> commodityList = commodityMapper.selectList(commodityQueryWrapper);
          shopmap.put("shop",shops);
          for(Commodity commoditys : commodityList){
              commodity.add(commoditys);
          }
          shopmap.put("commodity",commodity);
          dateList.add(shopmap);
        }
        List<Object> List = PageUtil.startPage(dateList,pageNum,pageSize);
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(List);
        wrapper.getData().put("data",List);
        wrapper.getData().put("total",dateList.size());
        return wrapper;
    }

    public ResponseWrapper getShoBysId(Integer sId, Integer pageSize,Integer pageNum,Integer compageSize,Integer compageNum){
        Shop shop = shopMapper.selectById(sId);
        Page<Map> page = new Page<>(pageNum,pageSize);
        IPage<Map> iPage = sCommentMapper.findCommentBysid(page,sId);
        List<Map> list = iPage.getRecords();
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
        wrapper.getData().put("shop",shop);
        wrapper.getData().put("comment",list);
        wrapper.getData().put("total",iPage.getTotal());
        wrapper.getData().put("pageSize",iPage.getSize());
        wrapper.getData().put("pageNum",iPage.getCurrent());
        QueryWrapper<Commodity> commodityQueryWrapper = new QueryWrapper<>();
        commodityQueryWrapper.eq("s_id",sId).orderByDesc("c_id");
        Page<Commodity> compage = new Page<>(compageNum,compageSize);
        IPage<Commodity> commodityIPage = commodityMapper.selectPage(compage,commodityQueryWrapper);
        List<Commodity> commodityList = commodityIPage.getRecords();
        wrapper.getData().put("commodityList",commodityList);
        wrapper.getData().put("comtotal",commodityIPage.getTotal());
        wrapper.getData().put("compageSize",commodityIPage.getSize());
        wrapper.getData().put("compageNum",commodityIPage.getCurrent());
        return wrapper;
    }

    public ResponseWrapper deleteShop(Integer sId){
        if (sId == null){
            return ResponseWrapper.markError();
        }else {
            QueryWrapper<Commodity> commodityQueryWrapper = new QueryWrapper<>();
            commodityQueryWrapper.eq("s_id",sId);
            List<Commodity> commodityList = commodityMapper.selectList(commodityQueryWrapper);
            for (Commodity commodity : commodityList){
                commodityMapper.deleteById(commodity.getCId());
            }
            int result = shopMapper.deleteById(sId);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else{
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper upload(MultipartFile file) throws IOException {
        //上传路径
        String uploadPath = "D:\\web\\tour\\file\\veido\\";
        /*获取文件名*/
        String getFileName = file.getOriginalFilename();
        String str1=getFileName.substring(0, getFileName.indexOf("."));
        String fileCategory=getFileName.substring(str1.length()+1, getFileName.length());
        if (fileCategory.equals("mp4") || fileCategory.equals("flv") ||  fileCategory.equals("avi")|| fileCategory.equals("wmv")){
            String filePath =uploadPath + getFileName;
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath));
            outputStream.write(file.getBytes());
            outputStream.flush();
            outputStream.close();
            String veidourl = "http://111.75.252.147:8888/veido/"+getFileName;
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(veidourl);
            wrapper.getData().put("veidourl",veidourl);
            return wrapper;
        }else {
            return ResponseWrapper.markError();
        }
    }

}
